package _10综合案例图书管理系统;

import java.net.IDN;
import java.util.*;

//图书管理系统主页
public class BookManage {
    private static HashMap<String, ArrayList<Book>> bookMap = new HashMap<>();
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        //测试数据
        ArrayList<Book> mz = new ArrayList<>();
        mz.add(new Book("西游记",28.0));
        mz.add(new Book("水浒传",24.0));

        ArrayList<Book> it = new ArrayList<>();
        it.add(new Book("java入门",99.0));
        it.add(new Book("php入门",88.0));

        bookMap.put("名著",mz);
        bookMap.put("IT",it);
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到图书管理系统--------");
            System.out.println("1.查看所有书籍");
            System.out.println("2.添加图书");
            System.out.println("3.删除图书");
            System.out.println("4.修改图书");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择：");


            String choose = sc.nextLine();
            switch (choose) {
                    case "1":
                        //查询图书
                        showBook();
                        break;
                    case "2":
                        //添加图书
                        addBook();
                            break;
                    case "3":
                    //删除图书
                        delBook();
                        break;
                    case "4":
                    //修改图书
                        updateBook();
                        break;
                    case "5":
                        System.out.println("谢谢使用");
                        return; //结束main方法
                     default:
                            System.out.println("你输入的选择无效，请重新输入");
                            break;
                    }
            }

        }

        //查看图书
    public static void showBook(){
        System.out.println("类型      书名      价格");
        Set<String> keySet = bookMap.keySet();
        for (String s : keySet) {
            System.out.println(s);
            ArrayList<Book> books = bookMap.get(s);
            for (Book book : books) {
                System.out.println("        " + book.getName() + "      " + book.getPrice());
            }
        }
    }
    public static void addBook(){
        System.out.println("请输入书籍类型：");
        String addBookType = sc.nextLine();
        System.out.println("请输入书籍名称：");
        String bookName = sc.nextLine();
        System.out.println("请输入书籍价格：");
        double bookPrice = sc.nextDouble();
        //如果图书类型存在
        if (bookMap.containsKey(addBookType)) {
            //判断书籍是否存在
            ArrayList<Book> books = bookMap.get(addBookType);
            if (selectByBookName(books,bookName)!=null) {
                System.out.println("添加失败，书籍已存在");
            }else {
                books.add(new Book(bookName, bookPrice));
                System.out.println("添加书籍成功");
            }
        }else {
            ArrayList<Book> books = new ArrayList<>();
            books.add(new Book(bookName,bookPrice));
            bookMap.put(addBookType,books);
            System.out.println("添加书籍成功");
        }
    }
    public static void delBook(){
        System.out.println("请输入需要删除的书籍类型");
        String delBookType = sc.nextLine();
        if (bookMap.containsKey(delBookType)) {
            //获取当前类型的图书集合
            ArrayList<Book> books = bookMap.get(delBookType);

            //如果书籍类型存在
            System.out.println("请输入需要删除的书籍名称");
            String delBookName = sc.nextLine();

            //判断图书是否存在,存在就删除
            Book book = selectByBookName(books, delBookName);
            if (book!=null) {
                books.remove(book);
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败，书籍名称不存在");
            }

        }else {
            System.out.println("书籍类型不存在!");
        }
    }

    /**
     *
     */
    public static void updateBook(){
        System.out.println("请输入需要修改的书籍类型");
        String delBookType = sc.nextLine();
        if (bookMap.containsKey(delBookType)) {
            //获取当前类型的图书集合
            ArrayList<Book> books = bookMap.get(delBookType);

            //如果书籍类型存在
            System.out.println("请输入需要修改的书籍名称");
            String delBookName = sc.nextLine();

            //判断图书是否存在
            Book book = selectByBookName(books, delBookName);
            if (book!=null) {
                System.out.println("请输入书名");
                String bookname = sc.nextLine();
                book.setName(bookname);
                System.out.println("请输入价格");
                double bookPrice = sc.nextDouble();
                book.setPrice(bookPrice);
                System.out.println("修改成功");
            }else {
                System.out.println("书籍名称不存在");
            }

        }else {
            System.out.println("书籍类型不存在!");
        }


    }

    /**
     * 根据书名查询书籍是否存在
     * @param books
     * @param bookName
     * @return
     */
    public static Book selectByBookName(ArrayList<Book> books,String bookName){
        for (Book book : books) {
            if (book.getName().equals(bookName)){
                //书籍存在
                return book;
            }
        }
        return null;
    }





}


